package controlLayer;

import java.sql.SQLException;
import java.util.List;

import utilLayer.AlreadyExistsException;
import modelLayer.ClassRoom;
import dbLayer.DBClassRoom;
import dbLayer.DBClassRoomIF;
import dbLayer.DBConnection;


/**
 * This class is a control layer class of the MSAS system. It functions as the link between 
 * our database and the interface. This class is limited to working with the ClassRoom class,
 * and whichever other classes ClassRoom is associated with. There is no direct connection between
 * this class and our database.
 * 
 * @author Danny M�lgaard, Anna Kelemen, Jakob Korsgaard
 * 		   
 * @version 1.3.1
 * 					1.3.1: 		Added more extensive documentation. Verified that
 * 								information contained in the Design Class Diagram,
 * 								is in accordance with the information contained in this
 * 								class.
 *					1.3.2:		Try&catch and throws Exception added to the constructor in order to catch
 *								a NullPointerException and to avoid the program from crashing
 *								if there is no connection to the database. 					
 */
public class ClassRoomCtr {
	
	private DBClassRoomIF dbClassRoomIF;
	
	/**
	 * Creates an instance of ClassRoomCtr. Initializes an instance of DBClassRoom.
	 * @throws Exception
	 */
	public ClassRoomCtr() throws Exception {
		try {
			this.dbClassRoomIF = new DBClassRoom();
		}
		catch (Exception e) {
			throw new Exception();
		}
		
	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the insertion of a new row in the database. Begins a transactions upon
	 * calling the dbLayer method, and rolls it back should anything that could result in dataloss,
	 * or unintended data manipulation occur.
	 * 
	 * @param	name		String	class room name.
	 * @param	purpose		String	what the class room is used for.
	 * @throws	Exception			throws SQL exceptions if the query isn't
	 * 								legal.
	 * @return	rc			int		count of rows affected by the query execution in the database
	 */
	public int createClassRoom(String name, String purpose) throws Exception {

		int rc = -1;
		try {
			DBConnection.startTransaction();
			rc = this.dbClassRoomIF.insert(name, purpose);
			DBConnection.commitTransaction();
		}
		catch (SQLException e) {
			DBConnection.rollbackTransaction();
			throw new AlreadyExistsException(name);
		}
		catch (Exception e) {
			DBConnection.rollbackTransaction();
			System.out.println("DEBUG MESSAGE [CLASSROOMCTR]: createClassRoom(): " + e);
		}
		
		return rc;
				
	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the search of an existing row in the database.
	 * 
	 * @param	name		String		class room name.
	 * @throws				Exception	throws SQL exceptions if the query isn't
	 * 									legal.
	 * @return	cr			ClassRoom	returns the classRoom matching the search.
	 */
	public ClassRoom findClassRoom(String name) throws Exception {
		
		ClassRoom cr = null;
		try {
			cr = this.dbClassRoomIF.find(name);
		}
		catch (Exception e) {
			System.out.println("DEBUG MESSAGE [CLASSROOMCTR]: findClassRoom(): " + e);
		}
		
		return cr;
		
	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the search of an existing row in the database.
	 * 
	 * @param	id			int			class room id.
	 * @throws				Exception	throws SQL exceptions if the query isn't
	 * 									legal.
	 * @return	cr			ClassRoom	returns the classRoom matching the search.
	 */
	public ClassRoom findById(int id) throws Exception {

		ClassRoom cr = null;
		try {
			cr = this.dbClassRoomIF.findById(id);
		}
		catch (Exception e) {
			System.out.println("DEBUG MESSAGE [CLASSROOMCTR]: findById(): " + e);
		}
		
		return cr;
		
	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the search of all existing rows in the database. 
	 * 
	 * @throws				Exception		throws SQL exceptions if the query isn't
	 * 										legal.
	 * @return	list		List<ClassRoom>	returns a list of all class rooms.
	 */
	public List<ClassRoom> getAll() throws Exception {
		
		List<ClassRoom> list = this.dbClassRoomIF.getAll();
		try {
			list = this.dbClassRoomIF.getAll();
		}
		catch (Exception e) {
			System.out.println("DEBUG MESSAGE [CLASSROOMCTR]: getAll(): " + e);
		}
		
		return list;
		
	}

	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the renewal of an existing row in the database. Begins a transactions upon
	 * calling the dbLayer method, and rolls it back should anything that could result in data loss,
	 * or unintended data manipulation occur.
	 * 
	 * @param	id			int			class room id.
	 * @param	name		String		the new value of the name column.
	 * @param	purpose		String		the new value of the purpose column.
	 * @throws				Exception	throws SQL exceptions if the query isn't
	 * 									legal.
	 * @return	rc			int			count of rows affected by the query execution in the database
	 */
	public int updateClassRoom(int id, String name, String purpose) throws Exception {
		
		int rc = -1;
		try {
			DBConnection.startTransaction();
			rc = this.dbClassRoomIF.update(id, name, purpose);
			DBConnection.commitTransaction();
		}
		catch (SQLException e) {
			DBConnection.rollbackTransaction();
			throw new AlreadyExistsException(name);
		}
		catch (Exception e) {
			System.out.println("DEBUG MESSAGE [CLASSROOMCTR]: updateClassRoom(): "+ e);
			DBConnection.rollbackTransaction();
		}
		
		return rc;
		
	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the removal of an existing row in the database. Begins a transactions upon
	 * calling the dbLayer method, and rolls it back should anything that could result in data loss,
	 * or unintended data manipulation occur.
	 * 
	 * @param	name		String		the name value in the row to be removed.
	 * @throws	Exception				throws SQL exceptions if the query isn't
	 * 									legal.
	 * @return	rc			int			count of rows affected by the query execution in the database
	 */
	public int removeClassRoom(String name) throws Exception {
		
			int rc = -1;
			try {
				DBConnection.startTransaction();
				rc = this.dbClassRoomIF.remove(name);
				DBConnection.commitTransaction();
			}
			catch (Exception e) {
				System.out.println("DEBUG MESSAGE [CLASSROOMCTR]: removeClassRoom(): " + e);
				DBConnection.rollbackTransaction();
			}
			
			return rc;
			
	}
	
}
